"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 建表Option选项无序及重复
Case Name   : 频繁执行建表语句，验证可靠性
Create At   : 2023.09.08
Owner       : z@wan005
Description :
    1.创建b库
    2.b库下查询dolphin插件
    3.建表100次
    4.清理环境
Expect      :
    1.成功
    2.自动加载成功,无需手动创建
    3.执行成功，通过率100%
    4.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class TableTest0033(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0001'
        self.tb_name = 'tb_plugin0001'

    def test_dolphin(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        text = '---step1:创建b库;expect:成功---'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '---step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step3:建表100次;expect:执行成功，通过率100%--'
        self.log.info(text)
        for i in range(100):
            sql_cmd = f"drop table if exists {self.tb_name};" \
                      f"create table {self.tb_name}" \
                      f"(idx integer primary key,log varchar(20)) " \
                      f"compress with(orientation=row,fillfactor=25) " \
                      f"nocompress tablespace pg_default " \
                      f"tablespace pg_default;"
            self.log.info(sql_cmd)
            msg = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
            self.log.info(msg)
            self.assertIn('CREATE TABLE', msg, '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境;expect:清理环境完成--'
        self.log.info(text)
        drop_cmd = f"drop database if exists {self.db_name};"
        drop_res = self.sh_primary.execut_db_sql(drop_cmd)
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
